package com.gasstion.controller;

import com.gasstion.dao.PermissionDao;
import com.gasstion.dao.RoleDao;
import com.gasstion.dao.RolePermissionDao;
import com.gasstion.dao.UserRoleDao;
import com.gasstion.entity.Permission;
import com.gasstion.entity.Role;
import com.gasstion.entity.UserRole;
import com.gasstion.pojo.PermissionBean;
import com.gasstion.pojo.StateBean;
import com.gasstion.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/role")
@Scope("prototype")
public class RoleController {
//    @Autowired
//    private RoleService roleService ;
    @Autowired
    private RoleDao roleDao ;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    public RolePermissionDao rolepermissionDao;
    @Autowired
    public PermissionDao permissionDao;

    @RequestMapping("/query")
    @ResponseBody
    public List<Role> query(int id){
        //查询当前用户所有拥有的角色
        UserRole userRole = userRoleDao.getRoleByUserId(id) ;
        //查询所有角色
        List<Role> list = roleDao.query() ;
        if(userRole!=null){
            for(Role r:list){
                if(userRole.getRid() == r.getId()){
                    r.setIschecked(true);
                }
            }
        }
        return list ;
    }

    @RequestMapping("/querySearch")
    @ResponseBody
    public Map<String,Object> querySearch(String rname,int offset,int limit){
        Map<String,Object> map=new HashMap<>();
        map.put("rows",roleDao.querySearch(rname,offset,limit));
        map.put("total",roleDao.getTotal(rname));
        return map;
    }
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Object> checkName(String rname){
        boolean flag;
        int row=roleDao.checkName(rname);
        if(row>0){
            flag=false;
        }else{
            flag=true;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("valid",flag);
        map.put("rname",rname);
        return map;
    }
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(Role role, int pn){
        roleDao.add(role);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",roleDao.querySearch("",(pn-1)*2,2));
        map.put("total",roleDao.getTotal(""));
        return map;
    }
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(Role role,int pn){
        roleDao.update(role);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",roleDao.querySearch("",(pn-1)*2,2));
        map.put("total",roleDao.getTotal(""));
        return map;
    }
    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> delUser(String ids,int pn,String rname){
        String[] ss=ids.split("-");
        int[] myids=new int[ss.length];
        for(int i=0;i<ss.length;i++){
            myids[i]=Integer.parseInt(ss[i]);
        }
        roleDao.del(myids);
        int offset=0;
        int count=roleDao.getTotal(rname);
        int pg=0;
        if(count%2==0){
            pg=count/2;
        }else{
            pg=count/2+1;
        }
        if(pn==pg){
            offset=(pn-1)*2;
        }else{
            offset=(pn-2)*2;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("rows",roleDao.querySearch(rname,offset,2));
        map.put("total",count);
        return map;
    }

    @RequestMapping("/toPer")
    @ResponseBody
    public List<PermissionBean> perBean(int rId){
        //获取当前用户所拥有的权限
        List<Permission> listAll= rolepermissionDao.queryByRid(rId);
        //获取根节点
        List<PermissionBean> pbList= permissionDao.query(0);

        int rid;
        for(PermissionBean pb:pbList){
            rid= pb.getId();
            //获取二级节点
            List<PermissionBean> pbList2=permissionDao.query(rid);
            pb.setNodes(pbList2);
            StateBean stateBean=new StateBean();
            pb.setState(stateBean);
        }
        //回显当前角色所拥有的权限
        for(PermissionBean pb: pbList){//循环根节点
            StateBean stateBean=new StateBean();
            for(Permission per:listAll){//循环用户拥有的权限
                if(pb.getId()==per.getPid()){
                    stateBean.setChecked(true);
                }
            }
            pb.setState(stateBean);
            //所有的子节点
            List<PermissionBean> listSon=pb.getNodes();
            for(PermissionBean son:listSon){//循环子节点
                StateBean stateBean1=new StateBean();
                for(Permission per2:listAll){//循环用户拥有的权限
                    if(son.getId()==per2.getPid()){
                        stateBean.setChecked(true);
                        stateBean1.setChecked(true);
                    }
                }
                son.setState(stateBean1);
            }
        }
        return pbList;
    }
}
